
#include <My_Inc/APP/APP_ACLoad_Sys.h>

static inline void Open_Loop(InterruptController *controller); // 开环模式 (LoopParamter -> LoopParameter)
System             g_acLoadSystem = {
    // 初始化 controller 数组
                .controller = {// --- 初始化 controller[0] (逆变器控制器) ---
                   [0] = {.monitor =
                    {
                                              .is_grid_over_voltage = Nonactivated,
                                              .is_inductor_over_current = Nonactivated,
                                              .is_bus_under_voltage = Nonactivated,
                                              .is_bus_over_voltage = Nonactivated,
                                              .fault_detected = Nonactivated,
                                              .fault_count = 0,
                                              .fault_time = 0,
                                              .device_state = DEVICE_STATE_NORMAL,
                                              .protection_state = PROT_STATE_NORMAL,
                                              .error_code = PROT_STATE_NORMAL,
                                              .state_trace_index = 0,
                                              .bus_over_voltage_threshold = 55.0f,
                                              .bus_under_voltage_threshold = 31.112f,
                                              .grid_over_voltage_threshold = 30.0f * _SQRT2,
                                              .il_over_current_threshold = 7.0f*_SQRT2,
                              },
                                      .loop_sm =
                                          {
                                              .current_state = STATE_IDLE,
                                              .event = EVENT_LOOP_NONE,
                                              .start_flag = Nonactivated,
                                              .stop_flag = Nonactivated,
                              },
                                      .pwm =
                                          {
                                              .pwm_flag = Nonactivated,
                                              .pwm_gpio_flag = Nonactivated,
                                              .pwm_en_flag = Nonactivated,
                                              .pwm_enable_gpio_port = PWM_ENABLE2_GPIO_Port,
                                              .pwm_enable_pin = PWM_ENABLE2_Pin,
                                              .mod_wave = 0.0f,
                                              .pwm_width = {0U, 0U},
                                              .timer = {(uint32_t)LL_HRTIM_TIMER_E, (uint32_t)LL_HRTIM_TIMER_F},
                                              .output_p = {(uint32_t)LL_HRTIM_OUTPUT_TE1, (uint32_t)LL_HRTIM_OUTPUT_TF1},
                                              .output_n = {(uint32_t)LL_HRTIM_OUTPUT_TE2, (uint32_t)LL_HRTIM_OUTPUT_TF2},
                              },
                                      .loop_mode = LOOP_MODE_OPEN,
                                      .param = {.lock_count = 0,
                                                .lock_timeout = 0,
                                                .phase_count = 0,
                                                .Phaseshift_angle = 0.0f,
                                                .theta = 0.0f,
                                                .sin_val = 0.0f,
                                                .cos_val = 0.0f,
                                                .vo_ref = 0.0f,
                                                .vo_actual = 0.0f,
                                                .il_ref = 0.0f,
												.pe_ref  = 0.0f,
												.qe_ref  = 0.0f,
                                                .il_actual = 0.0f,
                                                .udc_ref = 45.0f,
                                                .udc_actual = 0.0f,
                                                .pr_i = {&PR_50Hz_Inv_I, &PR_150Hz_Inv_I, NULL},
                                                .pr_v = {NULL, NULL, NULL},
                                                .pi_pe = NULL,
                                                .pi_qe = NULL,
                                                .peref = NULL,
                                                .qeref = NULL,
                                                .iref = NULL,
                                                .vref = NULL,
                                                .udcref = &PR_VREF_Inv,
                                                .pi_udc = &PI_Udc_Inv,
                                                .pll = NULL,
                                                .togi_v = NULL,
                                                .togi_i = NULL,
                                                .trap_udc = &FilterTrap_Udc,
                                                .SignalAnalysisr_v = &SignalAnalysisr_Inv,
                                                .SignalAnalysisr_i = NULL,
                                                .pq = NULL,
                                                .pr_ts = TS_20US,
                                                .pll_ts = TS_20US,
                                                .togi_v_ts = TS_20US,
                                                .togi_i_ts = TS_100US,
                                                .variableStep_ts = TS_20US,
                                                .pid_ts = TS_20US,
                                                .filtertrap_ts = TS_20US,
                                                .signalanalysisr_ts = TS_40US,
                                                .pq_ts = TS_20US,
                                                }
                                                ,
                                                    .adc = {
                                                        .adc1_res = {0U, 0U},
                                                        .adc2_res = {0U, 0U},
                                                        .adc3_res = {0U, 0U},
                                                        .adc4_res = {0U, 0U},
                                                        .adc5_res = {0U, 0U},
                                                },

}
,

    // --- 初始化 controller[1] (整流器控制器) ---
    [1] = {
        .monitor =
            {
                .is_grid_over_voltage = Nonactivated,
                .is_inductor_over_current = Nonactivated,
                .is_bus_under_voltage = Nonactivated,
                .is_bus_over_voltage = Nonactivated, // 补充初始化
                .fault_detected = Nonactivated,
                .fault_count = 0,
                .fault_time = 0,
                .device_state = DEVICE_STATE_NORMAL,
                .protection_state = PROT_STATE_NORMAL,
                .error_code = PROT_STATE_NORMAL,

                .bus_over_voltage_threshold = 55.0f,
                .bus_under_voltage_threshold = 31.112f,
                .grid_over_voltage_threshold = 26.0f * _SQRT2,
                .il_over_current_threshold = 6.0f*_SQRT2,
            },
        .loop_sm =
            {
                .current_state = STATE_IDLE,
                .event = EVENT_LOOP_NONE,
                .start_flag = Nonactivated,
                .stop_flag = Nonactivated,
            },
        .pwm =
            {
                .pwm_flag = Nonactivated,
                .pwm_enable_gpio_port = PWM_ENABLE1_GPIO_Port,
                .pwm_enable_pin = PWM_ENABLE1_Pin,
                .pwm_gpio_flag = Nonactivated,
                .pwm_en_flag = Nonactivated,
                .mod_wave = 0.0f,
                .pwm_width = {0U, 0U},
                .timer = {(uint32_t)LL_HRTIM_TIMER_B, (uint32_t)LL_HRTIM_TIMER_C},
                .output_p = {(uint32_t)LL_HRTIM_OUTPUT_TB1, (uint32_t)LL_HRTIM_OUTPUT_TC1},
                .output_n = {(uint32_t)LL_HRTIM_OUTPUT_TB2, (uint32_t)LL_HRTIM_OUTPUT_TC2},
            },
        .loop_mode = LOOP_MODE_OPEN,
        .param =
            {
                .lock_count = 0,
                .lock_timeout = 0,
                .phase_count = 0,
                .Phaseshift_angle = 0.0f,
                .theta = 0.0f,
                .sin_val = 0.0f,
                .cos_val = 0.0f,
                .vo_ref = 0.0f,
                .vo_actual = 0.0f,
                .il_ref = 0.0f,
				.pe_ref  = 0.0f,
				.qe_ref  = 0.0f,
                .il_actual = 0.0f,
                .udc_ref = 0.0f,
                .udc_actual = 0.0f,
                .pr_i = {&PR_50Hz_Rec_I, &PR_150Hz_Rec_I, &PR_250Hz_Rec_I},
                .pr_v = {&PR_50Hz_Rec_V, &PR_150Hz_Rec_V, &PR_250Hz_Rec_V},
                .pi_pe = &PI_Power_Pe,
                .pi_qe = &PI_Power_Qe,
                .qeref = &Power_Qe_Rec,
                .peref = &Power_Pe_Rec,
                .iref = &PR_IREF_Rec,
                .vref = &PR_VREF_Rec,
                .udcref = NULL,
                .pi_udc = NULL,
                .pll = &PLL_Rec,
                .togi_v = &TOGI_VGrid_Rec,
                .togi_i = &TOGI_IL_Rec,
                .trap_udc = &FilterTrap_Udc,
                .SignalAnalysisr_v = &SignalAnalysisr_Rec_v,
                .SignalAnalysisr_i = &SignalAnalysisr_Rec_i,
                .pq = &PQ_Rec,
                .pr_ts = TS_20US,
                .pll_ts = TS_20US,
                .togi_v_ts = TS_20US,
                .togi_i_ts = TS_20US,
                .variableStep_ts = TS_20US,
                .pid_ts = TS_20US,
                .filtertrap_ts = TS_20US,
                .signalanalysisr_ts = TS_40US,
                .pq_ts = TS_40US,
            },
        .adc =
            {
                .adc1_res = {0U, 0U},
                .adc2_res = {0U, 0U},
                .adc3_res = {0U, 0U},
                .adc4_res = {0U, 0U},
                .adc5_res = {0U, 0U},
            },

}
}
,

    // --- 初始化顶层系统模式 ---
    .task =
        {
            .task_20us_flag = Nonactivated,
            .task_40us_flag = Nonactivated,
            .task_100us_flag = Nonactivated,
            .task_20us_count = 0U,
            .task_40us_count = 0U,
            .task_100us_count = 0U,
},
    .wock_mode = WOCK_MODE_STOP, .device_mode = DEVICE_MODE_STOP, .sys_state = SYS_STATE_INIT,.switch_flag = Nonactivated,
}
;
/******************环路运算*************************/
// Current_Loop 参数类型和内部访问修正
// Open_Loop 参数类型和内部访问修正
static volatile float mo = 0.5f;
static inline void    Open_Loop(InterruptController *InterruptController)
{
    // 获取角度 (访问 LoopParameter 成员)
    InterruptController->param.phase_count = (InterruptController->param.phase_count + 1) % SIZE_SINE;
    InterruptController->param.theta = InterruptController->param.phase_count * SIZE_COEFFICIENT;
    Cordic_SinCos_Inline(InterruptController->param.theta, &InterruptController->param.sin_val,
                         &InterruptController->param.cos_val);
    // 计算调制波 (访问 LoopParameter 成员)
    InterruptController->pwm.mod_wave = (0.5f * InterruptController->param.sin_val);
}

float ADC1_result_temp = 0.0f;
float ADC2_result_temp = 0.0f;
float ADC3_result_temp = 0.0f;
float ADC4_result_temp = 0.0f;
float ADC5_result_temp = 0.0f;

#define Test_Mode 0
void HRTIM_MASTER_COMP1_IRQ(void)
{
    LL_GPIO_SetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);

    /**********ADC数据采集********************/
    // 采集 ADC1, ADC2, ADC3, ADC4, ADC5
    FILTER_MOVAVG(g_acLoadSystem.controller[0].adc.adc5_res[0], ADC5_result[0], 2);
    /**********ADC数据处理********************/
    g_acLoadSystem.controller[0].param.vo_actual = (0.0223667005f * ADC1_result[0] - 45.7854012348f);  // 逆变器侧电压
    g_acLoadSystem.controller[0].param.il_actual = (-0.0060779344f * ADC2_result[0] + 12.4898872822f); // 逆变器侧电流
    g_acLoadSystem.controller[1].param.udc_actual = g_acLoadSystem.controller[0].param.udc_actual =
        (0.0153092478f * g_acLoadSystem.controller[0].adc.adc5_res[0] + 0.0369912228f);                // 母线电压
    g_acLoadSystem.controller[1].param.vo_actual = (0.0202033137f * ADC3_result[0] - 41.3539084732f);  // 整流器侧电压
    g_acLoadSystem.controller[1].param.il_actual = (-0.0059273978f * ADC4_result[0] + 12.3025010994f); // 整流器侧电流

    /**********保护处理********************/
#if Test_Mode == 0
    Interrupt_Protection(&g_acLoadSystem.controller[0]);
    Interrupt_Protection(&g_acLoadSystem.controller[1]);

    /**********故障处理********************/
    if ((g_acLoadSystem.controller[0].param.SignalAnalysisr_v->Vrms < 23.0f) ||
        (g_acLoadSystem.controller[0].monitor.fault_detected == Activated) ||
        (g_acLoadSystem.controller[1].monitor.fault_detected == Activated)) // 发生错误
    {

        LL_GPIO_ResetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[0].pwm.pwm_enable_gpio_port,
                               g_acLoadSystem.controller[0].pwm.pwm_enable_pin);
        LL_GPIO_ResetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[1].pwm.pwm_enable_gpio_port,
                               g_acLoadSystem.controller[1].pwm.pwm_enable_pin);

        g_acLoadSystem.controller[0].pwm.pwm_gpio_flag = Nonactivated;
        g_acLoadSystem.controller[1].pwm.pwm_gpio_flag = Nonactivated;

        g_acLoadSystem.controller[0].loop_sm.current_state = STATE_IDLE;
        g_acLoadSystem.controller[1].loop_sm.current_state = STATE_IDLE;

        LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin); // 故障灯亮起
        LL_GPIO_ResetOutputPin(LED2_GPIO_Port, LED2_Pin); // 故障灯亮起

        g_acLoadSystem.sys_state = SYS_STATE_INIT;
    }
    else
    {
        if (g_acLoadSystem.controller[0].monitor.fault_detected == Nonactivated &&
            (g_acLoadSystem.controller[0].param.SignalAnalysisr_v->Vrms > 24.0f))
        {
            LL_GPIO_SetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[0].pwm.pwm_enable_gpio_port,
                                 g_acLoadSystem.controller[0].pwm.pwm_enable_pin);
            LL_GPIO_SetOutputPin(LED1_GPIO_Port, LED1_Pin); // 故障灯关闭

            g_acLoadSystem.controller[0].pwm.pwm_gpio_flag = Activated;
        }
        if (g_acLoadSystem.controller[1].monitor.fault_detected == Nonactivated)
        {
            LL_GPIO_SetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[1].pwm.pwm_enable_gpio_port,
                                 g_acLoadSystem.controller[1].pwm.pwm_enable_pin);

            LL_GPIO_SetOutputPin(LED2_GPIO_Port, LED2_Pin); // 故障灯关闭
            g_acLoadSystem.controller[1].pwm.pwm_gpio_flag = Activated;
        }
    }

#else
//    if (g_acLoadSystem.controller[0].monitor.fault_detected == Nonactivated /*&&
//        (g_acLoadSystem.controller[0].param.SignalAnalysisr_v->Vrms > 21.0f)*/)
//    {
//        LL_GPIO_SetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[0].pwm.pwm_enable_gpio_port,
//                             g_acLoadSystem.controller[0].pwm.pwm_enable_pin);
//        LL_GPIO_SetOutputPin(LED1_GPIO_Port, LED1_Pin); // 故障灯关闭
//
//        g_acLoadSystem.controller[0].pwm.pwm_gpio_flag = Activated;
//    }
//    if (g_acLoadSystem.controller[1].monitor.fault_detected == Nonactivated)
//    {
//        LL_GPIO_SetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[1].pwm.pwm_enable_gpio_port,
//                             g_acLoadSystem.controller[1].pwm.pwm_enable_pin);
//
//        LL_GPIO_SetOutputPin(LED2_GPIO_Port, LED2_Pin); // 故障灯关闭
//        g_acLoadSystem.controller[1].pwm.pwm_gpio_flag = Activated;
//    }
#endif
    /**********额外任务********************/

    Trap_Filter_Inline(g_acLoadSystem.controller[0].param.trap_udc,
                       g_acLoadSystem.controller[0].param.udc_actual); // 滤波处理
    TOGI_TO_DQ0_Inline(g_acLoadSystem.controller[1].param.togi_v, g_acLoadSystem.controller[1].param.vo_actual,
                       g_acLoadSystem.controller[1].param.pll->W, &g_acLoadSystem.controller[1].param.pll->Sine,
                       &g_acLoadSystem.controller[1].param.pll->Cosine);
    TOGI_TO_DQ0_Inline(g_acLoadSystem.controller[1].param.togi_i, g_acLoadSystem.controller[1].param.il_actual,
                       g_acLoadSystem.controller[1].param.pll->W, &g_acLoadSystem.controller[1].param.pll->Sine,
                       &g_acLoadSystem.controller[1].param.pll->Cosine);
    PLL_Process_Inline(g_acLoadSystem.controller[1].param.pll, g_acLoadSystem.controller[1].param.togi_v->Q);

    /**********环路任务********************/

#if Test_Mode == 1
//    LL_GPIO_SetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[1].pwm.pwm_enable_gpio_port,
//                         g_acLoadSystem.controller[1].pwm.pwm_enable_pin);
//    g_acLoadSystem.controller[1].pwm.pwm_gpio_flag = Activated; // 允许PWM
//    g_acLoadSystem.controller[1].pwm.pwm_en_flag = Activated;   // 允许PWM
//    Open_Loop(&g_acLoadSystem.controller[1]);
//    SPWM_Control_Inline(&g_acLoadSystem.controller[1].pwm);
//
//    LL_GPIO_SetOutputPin((GPIO_TypeDef *)g_acLoadSystem.controller[0].pwm.pwm_enable_gpio_port,
//                         g_acLoadSystem.controller[0].pwm.pwm_enable_pin);
//    g_acLoadSystem.controller[0].pwm.pwm_gpio_flag = Activated; // 允许PWM
//    g_acLoadSystem.controller[0].pwm.pwm_en_flag = Activated;   // 允许PWM
//    Open_Loop(&g_acLoadSystem.controller[0]);
//    SPWM_Control_Inline(&g_acLoadSystem.controller[0].pwm);
#endif

#if Test_Mode == 0

    switch (g_acLoadSystem.sys_state)
    {
    case SYS_STATE_INIT:

        if (g_acLoadSystem.controller[0].loop_sm.init_flag == Nonactivated) // 设置逆变器侧控制器初始状态
        {
            Pwm_Disable(&g_acLoadSystem.controller[0].pwm);
            Loop_Init(&g_acLoadSystem.controller[0].param);             // 初始化环路
            g_acLoadSystem.controller[0].loop_sm.init_flag = Activated; // 设置初始化标志
            g_acLoadSystem.controller[0].loop_sm.current_state = STATE_IDLE;
        }
        if (g_acLoadSystem.controller[1].loop_sm.init_flag == Nonactivated)
        {

            Pwm_Disable(&g_acLoadSystem.controller[1].pwm);
            Loop_Init(&g_acLoadSystem.controller[1].param);             // 初始化环路
            g_acLoadSystem.controller[1].loop_sm.init_flag = Activated; // 设置初始化标志
            g_acLoadSystem.controller[1].loop_sm.current_state = STATE_IDLE;
        }

        if ((g_acLoadSystem.controller[0].loop_sm.stop_flag == Activated))
        {
            /*清空标志位*/
            g_acLoadSystem.controller[0].loop_sm.stop_flag = Nonactivated;
            g_acLoadSystem.controller[0].loop_sm.init_flag = Nonactivated;
            g_acLoadSystem.controller[1].loop_sm.init_flag = Nonactivated;
            g_acLoadSystem.switch_flag = Nonactivated;
            g_acLoadSystem.sys_state = SYS_STATE_INIT; // 切换到下一个状态
            break;
        }
        /*状态转移*/

        if ((g_acLoadSystem.controller[0].loop_sm.start_flag == Activated) &&
            (g_acLoadSystem.controller[0].param.udc_actual < 42.0f)) // 等待母线电容放电完毕
        {
            /*清空标志位*/
            g_acLoadSystem.controller[0].loop_sm.start_flag = Nonactivated;
            g_acLoadSystem.controller[0].loop_sm.init_flag = Nonactivated;
            g_acLoadSystem.controller[1].loop_sm.init_flag = Nonactivated;

            g_acLoadSystem.controller[0].loop_sm.current_state = STATE_WAIT_CROSS_ZERO; // 设置逆变器状态为过零准备
            g_acLoadSystem.switch_flag = Activated;
            g_acLoadSystem.sys_state = SYS_STATE_SS; // 切换到下一个状态
            break;
        }

        break;
    case SYS_STATE_SS:

        /*状态转移*/
        if ((g_acLoadSystem.controller[0].loop_sm.stop_flag == Activated)) // 等待母线电容放电完毕
        {
            g_acLoadSystem.controller[0].loop_sm.stop_flag = Nonactivated; // 清空标志位
            Pwm_Disable(&g_acLoadSystem.controller[0].pwm);
            g_acLoadSystem.switch_flag = Nonactivated;
            g_acLoadSystem.sys_state = SYS_STATE_INIT; // 跳转到下一个状态
            break;
        }

        if ((g_acLoadSystem.device_mode == DEVICE_MODE_VOLTAGE) &&
            (g_acLoadSystem.wock_mode == WOCK_MODE_SOURCE ||
             g_acLoadSystem.wock_mode == WOCK_MODE_LOAD)) // 源模式或者载模式
        {
            // 如果模式为电压模式
            if ((g_acLoadSystem.controller[1].param.SignalAnalysisr_v->Vrms > 2.0f))
            {
                g_acLoadSystem.switch_flag = Nonactivated;
                g_acLoadSystem.sys_state = SYS_STATE_INIT; // 此时输出端口存在电压，返回初始化状态
            }
        }
        if (g_acLoadSystem.controller[0].param.udcref->state == VARIABLE_STEP_STATE_COMPLETE) // 软启动完成
        {
            g_acLoadSystem.sys_state = SYS_STATE_SSC; // 跳转到下一个状态
            break;
        }
        break;
    case SYS_STATE_SSC:

        /*状态转移*/
        if ((g_acLoadSystem.controller[0].loop_sm.stop_flag == Activated)) // 发生停止位被置位
        {
            g_acLoadSystem.controller[0].loop_sm.stop_flag = Nonactivated; // 停止位复位
            Pwm_Disable(&g_acLoadSystem.controller[0].pwm);                // 关波
            g_acLoadSystem.switch_flag = Nonactivated;
            g_acLoadSystem.sys_state = SYS_STATE_INIT; // 跳转到下一个状态
            break;
        }
        /*如果接收到模式设定*/

        //        /*状态转移*/
        //        if ((g_acLoadSystem.controller[1].loop_sm.start_flag == Activated))
        //        {
        //            /*清空标志位*/
        //            g_acLoadSystem.controller[1].loop_sm.start_flag = Nonactivated;
        //            g_acLoadSystem.controller[1].loop_sm.current_state = STATE_WAIT_CROSS_ZERO; // 设置整流器转移状态
        //            g_acLoadSystem.sys_state = SYS_STATE_WORKING;                               // 切换到下一个状态
        //
        //            break;
        //        }
        if((g_acLoadSystem.controller[0].param.trap_udc->notch.Yn < 42.0f))//判定为未开机成功，返回重新开机
        {
            Pwm_Disable(&g_acLoadSystem.controller[0].pwm);                // 关波
            g_acLoadSystem.switch_flag = Nonactivated;
            g_acLoadSystem.sys_state = SYS_STATE_INIT; // 跳转到下一个状态
            break;
        }
        g_acLoadSystem.controller[1].loop_sm.current_state = STATE_WAIT_CROSS_ZERO; // 设置整流器转移状态
        g_acLoadSystem.sys_state = SYS_STATE_WORKING;                               // 无条件跳转
        break;
    case SYS_STATE_WORKING:

        /*状态转移*/
        if ((g_acLoadSystem.controller[0].loop_sm.stop_flag == Activated) ||
            (g_acLoadSystem.controller[1].loop_sm.stop_flag == Activated)) // 发生停止位被置位
        {
            g_acLoadSystem.controller[0].loop_sm.stop_flag = Nonactivated;
            g_acLoadSystem.controller[1].loop_sm.stop_flag = Nonactivated;

            g_acLoadSystem.controller[0].loop_sm.init_flag = Nonactivated;
            g_acLoadSystem.controller[1].loop_sm.init_flag = Nonactivated;

            g_acLoadSystem.switch_flag = Nonactivated;
            /*网侧逆变处理 */
            Pwm_Disable(&g_acLoadSystem.controller[0].pwm);
            Pwm_Disable(&g_acLoadSystem.controller[1].pwm);
            g_acLoadSystem.sys_state = SYS_STATE_INIT; // 跳转到下一个状态
            break;
        }

        /*整流器运行 */
        switch (g_acLoadSystem.wock_mode) // 访问 device_mode
        {
        case WOCK_MODE_SOURCE:
            // 源模式
            switch (g_acLoadSystem.device_mode)
            {
            case DEVICE_MODE_VOLTAGE:                                  // 恒压源模式
                if (g_acLoadSystem.controller[1].param.vo_ref > 24.0f) // 限制输出电压
                    g_acLoadSystem.controller[1].param.vo_ref = 24.0f;
                else if (g_acLoadSystem.controller[1].param.vo_ref < 0.0f)
                    g_acLoadSystem.controller[1].param.vo_ref = 0.0f;
                RunVoltLoop_State_ProcessControl(&g_acLoadSystem.controller[1]);
                break;
            default:
                break;
            }
            break;
        case WOCK_MODE_LOAD:
            // 负载模式
            switch (g_acLoadSystem.device_mode)
            {
            case DEVICE_MODE_VOLTAGE: // 恒压源模式
                //                if (g_acLoadSystem.controller[1].param.vo_ref > 24.0f) // 限制输出电压
                //                    g_acLoadSystem.controller[1].param.vo_ref = 24.0f;
                //                else if (g_acLoadSystem.controller[1].param.vo_ref < 0.0f)
                //                    g_acLoadSystem.controller[1].param.vo_ref = 0.0f;
                //                RunVoltLoop_State_ProcessControl(&g_acLoadSystem.controller[1]);
                break;
            case DEVICE_MODE_CURRENT: // 恒流源模式

                if ((g_acLoadSystem.controller[1].param.SignalAnalysisr_v->Vrms > 2.0f) &&
                    (g_acLoadSystem.controller[1].param.togi_v->D >
                     g_acLoadSystem.controller[1].param.SignalAnalysisr_v->Vrms) &&
                    (_isInRange(g_acLoadSystem.controller[1].param.togi_v->Q, -0.5f, 0.5f)))
                {
                    if (g_acLoadSystem.controller[1].param.il_ref > 5.0f) // 限制输出电压
                        g_acLoadSystem.controller[1].param.vo_ref = 5.0f;
                    else if (g_acLoadSystem.controller[1].param.vo_ref < -5.0f)
                        g_acLoadSystem.controller[1].param.vo_ref = -5.0f;
                    RunLoadCurrLoop_State_ProcessControl(&g_acLoadSystem.controller[1]);
                }
                break;
            case DEVICE_MODE_POWER: // 功率载模式
                if ((g_acLoadSystem.controller[1].param.SignalAnalysisr_v->Vrms > 2.0f) &&
                    (g_acLoadSystem.controller[1].param.togi_v->D >
                     g_acLoadSystem.controller[1].param.SignalAnalysisr_v->Vrms) &&
                    (_isInRange(g_acLoadSystem.controller[1].param.togi_v->Q, -0.5f, 0.5f)))
                {
                    if (g_acLoadSystem.controller[1].param.pe_ref > 120.0f) // 限制输出电压
                        g_acLoadSystem.controller[1].param.pe_ref = 120.0f;
                    else if (g_acLoadSystem.controller[1].param.pe_ref < -120.0f)
                        g_acLoadSystem.controller[1].param.pe_ref = -120.0f;
                    RunLoadPowerLoop_State_ProcessControl(&g_acLoadSystem.controller[1]);
                }
                break;
            default:
                break;
            }
            break;
        case WOCK_MODE_STOP:
            break;
        default:
            g_acLoadSystem.sys_state = SYS_STATE_INIT;
            g_acLoadSystem.switch_flag = Nonactivated;
            break;
        }
        break;
    default:
        g_acLoadSystem.sys_state = SYS_STATE_INIT;
        g_acLoadSystem.switch_flag = Nonactivated;
        break;
    }

    /*母线电压稳定环路*/
    RunPFCLoop_State_ProcessControl(&g_acLoadSystem.controller[0]);
#endif

    /**********分频业务处理********************/
    g_acLoadSystem.task.task_20us_flag = Activated;
    if (++g_acLoadSystem.task.task_40us_count == 2)
    {
        g_acLoadSystem.task.task_40us_count = 0;
        g_acLoadSystem.task.task_40us_flag = Activated;
    }
    if (++g_acLoadSystem.task.task_100us_count == 10)
    {
        g_acLoadSystem.task.task_100us_count = 0;
        g_acLoadSystem.task.task_100us_flag = Activated;
    }
    if (++g_acLoadSystem.task.task_100ms_count == 5000)
    {
        g_acLoadSystem.task.task_100ms_count = 0;
        g_acLoadSystem.task.task_100ms_flag = Activated;
    }
    if (++g_acLoadSystem.task.task_400ms_count == 20000)
    {
        g_acLoadSystem.task.task_400ms_count = 0;
        g_acLoadSystem.task.task_400ms_flag = Activated;
    }

    LL_GPIO_ResetOutputPin(GPIO_TEST1_GPIO_Port, GPIO_TEST1_Pin);
}
